package com.czkevin.guicontrolsapi.builtin;

import org.bukkit.entity.HumanEntity;

import java.util.function.Consumer;

public class AsyncWindow extends AlertWindow {

  private final AsyncStatesFetcher statesFetcher;

  private String textNotFinishedYet = "\u00a7cTask is not finished yet, \n\u00a7cplease wait for a moment! ";

  public AsyncWindow(String title, String message, AsyncStatesFetcher statesFetcher, Consumer<Boolean> callback) {
    super(AlertType.ATTENTION, title, message, callback);
    if(statesFetcher == null) {
      throw new IllegalArgumentException("stateFetcher is null");
    }
    this.statesFetcher = statesFetcher;
  }

  @Override
  public void onOpenInternal(HumanEntity humanEntity) {
  }

  public void setTextNotFinishedYet(String textNotFinishedYet) {
    this.textNotFinishedYet = textNotFinishedYet;
  }

  @Override
  protected boolean onCloseInternal(HumanEntity target) {
    if(!statesFetcher.isFinished()) {
      target.sendMessage(textNotFinishedYet);
      return false;
    }
    return super.onCloseInternal(target);
  }

  public interface AsyncStatesFetcher {
    boolean isFinished();
  }
}
